package com.ruoyi.project.stats.controller;

import com.ruoyi.project.stats.domain.*;
import com.ruoyi.project.stats.service.IStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author LoKa
 * @date 2020-09-17 16:43
 */
@Controller
@RequestMapping("/stats")
public class StatsController {

    @Autowired
    private IStatsService statsService;

    private String prefix = "stats";

    /**
     * 客户收款统计页面
     */
    @GetMapping("/customer/payment")
    public String paymentEcharts() {
        return prefix + "/paymentEcharts";
    }

    @RequestMapping("/customer/getPaymentStats")
    @ResponseBody
    public CustomerPaymentDataBean getPaymentStats() {
        String title = "订单收款统计";
        String[] legend = {"总额", "待收款"};
        String[] customers = statsService.selectDistinctCustomerInfo();
        Integer[][] datas = new Integer[legend.length][customers.length];
        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                int flag = "总额".equals(legend[i]) ? 0 : 1;
                datas[i][j] = statsService.getPaymentByCustomer(flag, customers[j]);
            }
        }
        CustomerPaymentDataBean customerPaymentDataBean = new CustomerPaymentDataBean(title, legend, customers, datas);
        return customerPaymentDataBean;
    }


    /**
     * 客户订单统计页面
     */
    @GetMapping("/customer/order")
    public String orderEcharts() {
        return prefix + "/orderEcharts";
    }

    /**
     * 按订单类型统计
     *
     * @return
     */
    @RequestMapping("/customer/getOrderStats")
    @ResponseBody
    public OrderTypeDataBean getOrderStats() {
        String title = "订单类型统计";
        String[] orderTypes = statsService.selectDistinctOrderType();
        String[] customers = statsService.selectDistinctCustomerInfo();
        Integer[][] datas = new Integer[orderTypes.length][customers.length];
        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                datas[i][j] = statsService.getCountByTypeAndCustomer(orderTypes[i], customers[j]);
            }
        }
        OrderTypeDataBean orderTypeDataBean = new OrderTypeDataBean(title, orderTypes, customers, datas);
        return orderTypeDataBean;
    }

    /**
     * 按订单金额统计
     */
    @RequestMapping("/customer/getAmountStats")
    @ResponseBody
    public OrderAmountDataBean getAmountStats() {
        String[] legend = {"订单金额"};
        String title = "订单金额统计";

        String[] customers = statsService.selectDistinctCustomerInfo();
        Integer[] datas = new Integer[customers.length];
        for (int i = 0; i < datas.length; i++) {
            datas[i] = statsService.getAmountCountByCustomer(customers[i]);
        }
        OrderAmountDataBean orderAmountDataBean = new OrderAmountDataBean(title, legend, customers, datas);
        return orderAmountDataBean;
    }

    /**
     * 项目完成度页面
     * @return
     */
    @GetMapping("/project/completion")
    public String projectEcharts() {
        return prefix + "/projectEcharts";
    }

    @RequestMapping("/project/getProjectCompletion")
    @ResponseBody
    public ProjectDataBean getProjectCompletion() {
        String[] legend = statsService.selectDistinctProjectPhase();
        ProjectDataBean pjb = new ProjectDataBean();
        Data[] dataArr = new Data[legend.length];
        for (int i = 0; i < legend.length; i++) {
            String phase = legend[i];
            int cnt = statsService.getCountByPhase(phase);
            Data data = new Data(phase, cnt);
            dataArr[i] = data;
        }
        pjb.setLegend(legend);
        pjb.setDatas(dataArr);
        return pjb;
    }


    /**
     * 任务完成度页面
     *
     * @return
     */
    @GetMapping("/task/completion")
    public String taskEcharts() {
        return prefix + "/taskEcharts";
    }

    @RequestMapping("/task/getTaskCompletion")
    @ResponseBody
    public TaskDataBean getTaskCompletion() {
        String title = "任务完成度";
        String[] statuses = statsService.selectDistinctTaskStatus();
        String[] urgencies = statsService.selectDistinctTaskUrgency();

        Integer[][] nums = new Integer[statuses.length][urgencies.length];

        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums[i].length; j++) {
                nums[i][j] = statsService.getCountByStatusAndUrgency(statuses[i], urgencies[j]);
            }
        }
        String[] legend = new String[statuses.length];
        for (int i = 0; i < legend.length; i++) {
            if ("0".equals(statuses[i])) {
                legend[i] = "未完成";
            } else if ("1".equals(statuses[i])) {
                legend[i] = "已完成";
            } else if ("2".equals(statuses[i])) {
                legend[i] = "放弃";
            }
        }
        String[] xName = new String[urgencies.length];
        for (int i = 0; i < xName.length; i++) {
            if ("0".equals(urgencies[i])) {
                xName[i] = "一般";
            } else if ("1".equals(urgencies[i])) {
                xName[i] = "紧急";
            } else if ("2".equals(urgencies[i])) {
                xName[i] = "非常紧急";
            }
        }
        TaskDataBean taskDataBean = new TaskDataBean(title, legend, xName, nums);
        return taskDataBean;
    }

}
